package com.echo.im.server.netty.processor.factory;


import com.echo.im.common.enums.IMCmdType;
import com.echo.im.server.netty.processor.MessageProcessor;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  Processor 工厂
 * @author echo
 */
@Component
@RequiredArgsConstructor
public class BaseProcessorFactory implements ProcessorFactory {

    private final List<MessageProcessor<?>> processors;

    private Map<IMCmdType, MessageProcessor<?>> processorMap;

    @PostConstruct
    public void  init() {
        processorMap=new HashMap<>();
        for (MessageProcessor<?> processor : processors) {
            processorMap.put(processor.getCmdType(), processor);
        }
    }

    @Override
    public <T> MessageProcessor<T> createProcessor(IMCmdType cmd) {
        return (MessageProcessor<T>) processorMap.get(cmd);
    }
}
